Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@moebius/http-graceful-shutdown

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@moebius/http-graceful-shutdown

Gracefully terminates HTTP servers in Node.js

  • 1.1.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@moebius/http-graceful-shutdown

npm version

This package for Node.js provides you with easy to use facilities required to gracefully terminate HTTP servers.

It's designed to work with native http.Server implementation, so it could be used for any server build on top of it (i.e. Express and other). See examples below.

Features

  • Keeps track of all open connections and their status (active/idle)
  • Gracefully terminates all connections by terminating the idle ones right away and waiting for pending requests to complete
  • Very straightforward and simple API to use
  • Minimal working implementation possible without extraneous functionality
  • Written in TypeScript, but supports vanilla JavaScript

Problem

When your application's process is interrupted by the operating system (by passing SIGINT or SIGTERM signals) by default the server is terminated right away and all open connections are brutally severed. This means that if some client was in the process of sending or receiving data from your server it will encounter an error. This could easily lead to escalating errors down the chain and data corruption.

The better strategy would be to listen for interruption signals and to close connections gracefully by closing only idle connections right away and waiting for pending requests to properly complete.

This module solves exactly this problem (or at least the second part of it).

Installation

Install package with yarn:

yarn add @moebius/http-graceful-shutdown

Or npm:

npm i -S @moebius/http-graceful-shutdown

Usage

Vanilla Server

const http = require('http');  
const GracefulShutdownManager = require('@moebius/http-graceful-shutdown').GracefulShutdownManager;


const server = http.createServer(/** … */);
const shutdownManager = new GracefulShutdownManager(server);

server.listen(8080);

process.on('SIGTERM', () => {
  shutdownManager.terminate(() => {
    console.log('Server is gracefully terminated');
  });
});

Express Server

const express = require('express');
const GracefulShutdownManager = require('@moebius/http-graceful-shutdown').GracefulShutdownManager;


const app = express();

const server = app.listen(8080);

const shutdownManager = new GracefulShutdownManager(server);

process.on('SIGTERM', () => {
  shutdownManager.terminate(() => {
    console.log('Server is gracefully terminated');
  });
});

API

GracefulShutdownManager Class

  • construct (server: http.Server)

    Creates an instance of GracefulShutdownManager class.

    The server argument is a Node.js HTTP server instance, i.e. http.Server. If you are using Express, you can obtain it like this: const server = app.listen(/** … */);

  • terminate(callback: Function)

    Initiates graceful termination of the server by closing all idle connections first and then by waiting for pending requests to finish. New requests are not accepted after calling this.

Support

If you like this module please consider to add a star on GitHub repository.

Thank you!

License

The MIT License (MIT)

Copyright (c) 2017 Slava Fomin II, MOEBIUS FOUNDATION.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Package last updated on 10 Jul 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc